package com.kinon.norogamilk.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.kinon.norogamilk.R;
import com.kinon.norogamilk.bean.AdBean;
import com.kinon.norogamilk.util.ImageUtilsLoaderFile;

import java.util.ArrayList;

/**
 *
 */

public class AdViewPagerAdapter extends PagerAdapter {
    Context context;
    ArrayList<View> list;
    ArrayList<AdBean> adlist;

    public AdViewPagerAdapter(Context context, ArrayList<View> list,
                              ArrayList<AdBean> adlist) {
        super();
        this.context = context;
        this.list = list;
        this.adlist = adlist;
    }

    @Override
    public int getCount() {
        if (adlist != null) {
            return adlist.size();
        }
        return 0;
    }


    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        // TODO Auto-generated method stub
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        // ((ViewPager) container).removeView(list.get(position));
        // ((ViewPager)container).removeView(mImageViews[position%mImageViews.length]);

        int newPosition = position % list.size();
        ((ViewPager) container).removeView(list.get(newPosition));
    }

    @Override
    public Object instantiateItem(final View container, final int position) {

        String imgurl = adlist.get(position).getPic();
        if (!"".equals("imgurl") || imgurl != null) {
            View view = list.get(position);
            ImageView image = ((ImageView) view.findViewById(R.id.img_ad));
            // image.setBackground(cachedImage);
//		ImageUtilsLoaderFile.displayImage(context, adlist.get(position).getPic(), image,
//				R.drawable.img_cow_sorry);
            ImageUtilsLoaderFile.displayImage(context, adlist.get(position).getPic(), image,
                    0);

            ((ViewPager) container).removeView(list.get(position));
            ((ViewGroup) container).addView(list.get(position));
        }
        return list.get(position);

    }
}
